<?php
/**
 * 新闻资讯模块
 **/
namespace Mama\Controller;
use Think\Controller;

class NewsController extends Controller {
	public function __construct() {
        parent::__construct();
		//判断是否登陆
		if(!session('userid')){ //已登陆
			$this->redirect("index/index");
		}

		$this->assign('shoplist', S('shoplist_'. session('userid')));
    }
	
	/**
	 * 新闻列表
	 **/
    public function index(){
    	$ajax = I('get.ajax', 0);
    	$page = I('get.page', 1);
    	//条件
    	$shopid = I('get.shopid', 0);
    	if($shopid){
    		$where['shop_id'] = $shopid;
    	}
    	$visible = I('get.visible');
		if($visible != 'all' && !isset($visible)){
			$where['visible'] = $visible;
		}
		
    	$newsDao = D('News');
    	//总数
    	$count = $newsDao->getNewsCount($where);
    	$this->assign('count', $count);
    	//列表
    	$newslist = $newsDao->getNewsList($page, $where);
    	$this->assign('newslist', $newslist);
    	if($ajax){
    		$this->display('news_list', 'utf-8', 'html');
    	}else{
    		$this->display();
    	}
    }
	
	/**
	 * 微信菜单列表
	 **/
    public function menu(){
    	$this->display();
    }
	
	/**
	 * 新增新闻资讯
	 **/
	public function add(){
		if(IS_GET){
			$newid = I('get.newid', 0);
			if($newid){
				$info = D('News')->getNewsInfoById($newid);
				$this->assign('info', $info);
			}
			$this->display();
		}else{
			$data = I('post.');
			//判断参数
			if(strlen($data['title']) > 200 || strlen($data['title']) <= 0){
				echo 0;
			}
			
			if(strlen($data['content']) <= 0){
				echo 0;
			}
			
			//设置固定参数
			$data['admin_id'] = session('userid');
			$data['admin_nick'] = session('nickname');
			$data['visible'] = 1;
			$data['createtime'] = time();
			if(empty($data['newid'])){
				unset($data['newid']);
				$row = D('News')->addNewsInfo($data);
			}else{
				$newid = $data['newid'];
				unset($data['newid']);
				$row = D('News')->editNewsInfo($newid, $data);
			}
			
			if($row !== false){
				echo 1;
			}else{
				echo 0;
			}
		}
	}
	
	/**
	 * 删除新闻
	 **/
	public function dels(){
		$newid = I('post.newid', 0);
		//判断是否拥有权限
		$row = D('News')->deleteNewsInfo($newid);
		if($row !== false){
			echo 1;
		}else{
			echo 0;
		}
	}
}